const db = require('../config/db.js');
const inFile = require('../utils/inFile.js');
const activityModel = {
    //带分页查询,模糊查询
    GetActivityList: async ({pageCurrent,pageSize,ActivityKeyword}) =>{
        console.log(pageCurrent,'当前页');
        console.log(pageSize,'每页显示的条数');
        let current = (pageCurrent - 1) * pageSize || 0;
        let size = pageSize || 10;
        let sql1 = 'SELECT COUNT(*) AS total FROM activity;';
        let sql2 = `SELECT COUNT(*) AS total FROM activity WHERE CONCAT(title,address,content) LIKE CONCAT('%', ?, '%')`;
        let sql3 = `SELECT id,title,content,address,start_time,end_time,image,state FROM activity LIMIT ${current},${size}`;
        let sql4 = `SELECT id,title,content,address,start_time,end_time,image,state FROM activity WHERE CONCAT(title,address,content) LIKE CONCAT('%', ?, '%') LIMIT ${current},${size};`
        try {
            if(ActivityKeyword) {

                const res1 = await db.query(sql4, [ActivityKeyword]);
                const res2 = await db.query(sql2,[ActivityKeyword]);
                const [{total}]= res2[0];
                console.log(total, '获取活动列表总记录');
                console.log(res1[0], '获取活动列表记录分页带模糊查询');
                return {
                    list: res1[0],
                    total,
                }
            }else{
                const res1 = await db.query(sql1);
                const res2 = await db.query(sql3);
                const [{total}]= res1[0];
                console.log(total, '获取活动列表总记录');
                console.log(res2[0], '获取活动列表记录分页');
                return {
                    total,
                    list: res2[0]
                }
            }
        } catch (error) {
            console.log(error,'model层获活动列表数据出错啦');
        }
    },
    AddActivity:async({title,address,start_time,end_time,content,state,image},file)=>{
        const sql1 = "SELECT * FROM activity WHERE title = ? ";
        const sql2 = "INSERT INTO activity (title,address,start_time,end_time,content,state,image) VALUES (?,?,?,?,?,?,?);";
        const sql3 = "INSERT INTO activity (title,address,start_time,end_time,content,state) VALUES (?,?,?,?,?,?);";
        try {
            if (file) {
                const imgPath = inFile(file,'img/Activity/');
                if (title) {
                    const result1 = await db.query(sql1, [title]);
                    console.log(result1[0], 'result1[0]');
                    if (result1[0].length !== 0) {
                        return 0;
                    } else {
                        const result2 = await db.query(sql2, [title,address,start_time,end_time,content,state,imgPath]);
                        console.log(result2[0], 'result2[0]');
                        return 1;
                    }
                }
            }else{
                if (title) {
                    const result1 = await db.query(sql1, [title]);
                    console.log(result1[0], 'result1[0]');
                    if (result1[0].length !== 0) {
                        return 0;
                    } else {
                        const result2 = await db.query(sql3, [title,address,start_time,end_time,content,state]);
                        console.log(result2[0], 'result2[0]');
                        return 1;
                    }
                }
            }
        } catch (error) {
            console.log(error, 'ActivityModel添加活动失败啦');
        }
    },
    EditActivity: async ({id,title,address,start_time,end_time,content,state,image},file) => {
        const sql1 = "UPDATE activity SET title=?,address=?,start_time=?,end_time=?,content=?,state=?,image=? WHERE id=?;";
        const sql2 = "UPDATE activity SET title=?,address=?,start_time=?,end_time=?,content=?,state=? WHERE id=?;";
        console.log(file,'活动file对象');
        debugger;
        try {
            if (file) {
                const imgPath = inFile(file,'img/Activity/');
                if (id) {
                    const result2 = await db.query(sql1, [title,address,start_time,end_time,content,state,imgPath,id]);
                    console.log(result2[0], 'result2[0]');
                    return 1;
                }else{
                    return 2
                }
            }else{
                if (id) {
                    const result2 = await db.query(sql2, [title,address,start_time,end_time,content,state,id]);
                    console.log(result2[0], 'result2[0]');
                    return 1;
                }else{
                    return 2
                }
            }
        } catch (error) {
            console.log(error, 'activity修改活动失败啦');
        }
    },
    DeleteActivity: async ({id})=>{
        console.log(id,'删除活动id测试');
        const sql ='DELETE FROM activity WHERE id=?;';
        try {
            if(id){
                const res = await db.query(sql,[id]);
                console.log(res[0],'删除活动调试');
                return res[0];
            }else{
                return;
            }
        } catch (error) {
            return {
                errorMsg: error
            }
        }
    },
    //批量删除活动
    BatchDeleteActivity: async  ({idList}) => {
        console.log(idList,'收集idList的数组');
        const sql = `DELETE FROM activity WHERE id IN(${idList});`;
        try {
            if (idList.length>0){
                const res = await db.query(sql);
                console.log(res,'Model层批量删除活动调试');
                return res[0];
            }else{
                return ;
            }
        }catch (error){
            return{
                errorMsg:error
            }
        }
    },
    UpdateActivityState: async ({id,state})=> {
        console.log(id,state,'-----');
        const sql = 'UPDATE activity SET state=? WHERE id=?;';
        try {
            if(id){
                const res = await db.query(sql,[state,id]);
                console.log(res[0],'更新活动状态调试');
                return res[0];
            }else{
                return;
            }
        } catch (error) {
            return {
                errorMsg: error
            }
        }
    },
    GetActivityDetail:async ({id}) => {
        console.log(id,'活动id');
        const sql = 'SELECT title,address,start_time,end_time,content,state,image FROM activity WHERE id = ? ;';
        try {
            if(id){
                const res = await db.query(sql,[id]);
                console.log(res[0],'获取活动详情调试');
                return res[0];
            }else{
                return;
            }
        }catch (error) {
            console.log(error,'获取活动详情出错了——Model层');
        }
    },
    GetActivityPublishList: async ({pageCurrent,pageSize,ActivityKeyword}) =>{
        console.log(pageCurrent,'当前页');
        console.log(pageSize,'每页显示的条数');
        let current = (pageCurrent - 1) * pageSize || 0;
        let size = pageSize || 10;
        let sql1 = 'SELECT COUNT(*) AS total FROM activity WHERE state = 1 ;';
        let sql2 = `SELECT COUNT(*) AS total FROM activity WHERE CONCAT(title,address,content) LIKE CONCAT('%',?, '%')  AND state = 1`;
        let sql3 = `SELECT id,title,content,address,start_time,end_time,image,state FROM activity WHERE state = 1 LIMIT ${current},${size};`;
        let sql4 = `SELECT id,title,content,address,start_time,end_time,image,state FROM activity WHERE CONCAT(title,address,content) LIKE CONCAT('%',?, '%')  AND state = 1  LIMIT ${current},${size};`
        try {
            if(ActivityKeyword) {

                const res1 = await db.query(sql4, [ActivityKeyword]);
                const res2 = await db.query(sql2,[ActivityKeyword]);
                const [{total}]= res2[0];
                console.log(total, '获取活动列表总记录');
                console.log(res1[0], '获取活动列表记录分页带模糊查询');
                return {
                    list: res1[0],
                    total,
                }
            }else{
                const res1 = await db.query(sql1);
                const res2 = await db.query(sql3);
                const [{total}]= res1[0];
                console.log(total, '获取活动列表总记录');
                console.log(res2[0], '获取活动列表记录分页');
                return {
                    total,
                    list: res2[0]
                }
            }
        } catch (error) {
            console.log(error,'model层获活动列表数据出错啦');
        }
    },
};

module.exports = activityModel;